<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="/static/common/taglib.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>车次管理</title>
    <%@include file="../../public/commonCss.jsp" %>
    <%@include file="../../public/commonJs.jsp" %>
</head>
<body ms-controller="main" class="ms-controller">
<div class="layui-form" style="padding: 10px">
    <div class="layui-inline">
        <div class="layui-input-inline">
            <input type="text" class="layui-input" placeholder="搜索编号、姓名" id="t1" tabindex="1"
                   ms-duplex="searchKey.searchCode" data-duplex-changed="searchChange"/>
        </div>
    </div>
    <div class="layui-inline" style="margin-left: 10px;font-size: 16px" ms-repeat="selectUsers">
        <div class="layui-input-inline">
            <button class="layui-btn layui-btn-small">{{el.trainNum}} {{el.userName}}</button>
            <button class="layui-btn layui-btn-small layui-btn-danger" ms-on-click="$remove">删除</button>
        </div>
    </div>
    <table class="layui-table">
        <thead>
        <th>序号</th>
        <th>用户编号</th>
        <th>用户名</th>
        <th>部门</th>
        <th>选择</th>
        </thead>
        <tbody>
        <tr ms-repeat="tableData">
            <td>{{searchKey.pageSize*(searchKey.currentPage-1)+$index+1}}</td>
            <td>{{el.userNum}}</td>
            <td>{{el.username}}</td>
            <td>{{el.depName}}</td>
            <td>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="number" class="layui-input js_trainNum" style="width: 80px;height: 30px"
                               ms-duplex="el.trainNum"
                               placeholder="车组号">
                    </div>
                    <div class="layui-input-inline">
                        <button class="layui-btn layui-btn-primary layui-btn-small js_select"
                                ms-on-click="selectThis(el)">回车派班
                        </button>
                    </div>
                </div>
            </td>
        </tr>
        </tbody>

    </table>
    <!-- 分页 -->
    <div id="demo" class="fenye">
    </div>
    <blockquote class="layui-elem-quote">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input class="layui-input" type="text" placeholder="输入gsm1电话" ms-duplex="gsm1"/>
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input class="layui-input" type="text" placeholder="输入gsm2电话" ms-duplex="gsm2"/>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">叫班:</label>
            <div class="layui-input-inline">
                <input type="time" id="t3" class="layui-input js_focus" ms-duplex="remindTime"/>
            </div>
        </div>
    </blockquote>
    <!-- whitebox -->
</div>
<script type="text/javascript">
    var vm;
    var dateStr = "${dateStr}";
    var roadId = "${road.id}";
    $(function () {
        vm = avalon.define({
            $id: "main",
            tableData: [],//表格数据
            selectUsers: [],
            user: {userId: '', userName: '', trainNum: ''},
            remindTime: "${remindTime}",
            searchKey: {searchCode: "", currentPage: 1, pageSize: 10, totalPage: 0},
            gsm1: '${road.gsm1}',
            gsm2: '${road.gsm2}',
            searchChange: function (value) {
                vm.searchKey.searchCode = value;
                loadTableData();
            }
        });
        loadTableData();
        initKeyDown();
        loadGiveWorks();
    });

    function initKeyDown() {
        document.onkeydown = function (e) {
            var ev = document.all ? window.event : e;
            if (ev.keyCode == 13) {
                var t1 = $('#t1').is(":focus");
                var t3 = $('.js_trainNum').is(":focus");
                if (t1) {//搜索
                    loadSearchData();
                }
                if (t3) {//保存输入的人
                    $(":focus").parent().next().find(".js_select").click();
                    return
                }
                if (!(t1 || t3)) {
                    $('.js_trainNum').each(function (index, element) {
                        if ($(element).val().trim() != '') {
                            $(element).parent().next().find(".js_select").click();
                            return;
                        }
                    });
                }
            }
        }
    }

    var form = layui.form();

    //初始化分页插件的方法
    function loadTableData() {
        var dd = vm.$model.searchKey;
        ajaxUtil.postDealCode("${ctx}/giveWorkController/selectOnePageUser.action", dd, true, function (data) {
            $('#t1')[0].focus();
            vm.tableData = data.result;
            vm.searchKey.totalPage = data.totalPage;
            pageUtil.renderPage(vm.searchKey, loadTableData, "demo")
        })
    }

    //加载
    function loadGiveWorks() {
        var dataStr = {giveWorkId: '${giveWorkId}'};
        ajaxUtil.post("${ctx}/giveWorkController/selectAllRoadGiveWork.action", dataStr, false, function (data) {
            vm.selectUsers = data.giveWorks;
        })

    }

    //选择当前的用户
    function selectThis(el) {
        var trainNum = el.$model.trainNum;
        if (!trainNum || trainNum.trim() == '') {
            parent.layer.msg('车组号不能为空', {icon: 2, anim: 6});
            return;
        }
        var dataStr = {
            trainNum: trainNum
        }
        ajaxUtil.postDealCode("${ctx}/giveWorkController/checkTrainNum.action", dataStr, false, function (data) {
            if (!data) {
                parent.layer.msg('车组号不存在', {icon: 2, anim: 6});
            } else {
                vm.user.userName = el.$model.username;
                vm.user.userId = el.$model.id;
                vm.user.trainNum = trainNum;
                vm.selectUsers.push(vm.$model.user);
                saveAll();
            }
        });

    }

    //加载搜索的数据
    function loadSearchData() {
        loadTableData();
    }

    //保存所有
    function saveAll() {
        var dataStr = {
            users: JSON.stringify(vm.$model.selectUsers),
            roadId: roadId,
            dateStr: dateStr,
            gsm1: vm.$model.gsm1,
            gsm2: vm.$model.gsm2,
            remindTime: vm.$model.remindTime,
            giveWorkId: '${giveWorkId}'
        };
        ajaxUtil.post("${ctx}/giveWorkController/addUsersInRoad.action", dataStr, true, function (data) {
            parent.layer.closeAll();
            layerUtil.success("派班成功");
            parent.loadTableData();
        });
    }

    form.render();
</script>
</body>
</html>